// JavaScript Document
define(function(require, exports, module) {
	Number.prototype.option_add = {
		"regexpInt" : "^[0-9]+$",
		"regexpDouble" : "^[0-9]+\\.[0-9]+$",
		"chineseNumberList" : [
				[ "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" ],
				[ "拾", "佰", "仟" ], [ "万", "亿","兆" ], [ "元", "角", "分" ] ],
		"maxNum":999999999999999.9999
	}
	// 直接获得字符串
	Number.prototype.inChinese = function(obj) {
		var self = this;
		var obj=obj.toString();
        obj = $.trim(obj).replace(/\s+/g, "");
		var str;
        if (obj && obj.indexOf(",")) {
            obj = obj.split(",");
            obj = obj.join("");
        }
		var regexpI = RegExp(self.option_add.regexpInt, "g");
		var regexpD = RegExp(self.option_add.regexpDouble, "g");
		if (regexpI.test(obj)) {
			str = obj;
		} else if(regexpD.test(obj)){
			str = obj;
		} else {
			console.log("得到的数据既不是int也不是double");
			str="";
			return str;
		}


		if (str.length == 0)
			return "没有取得数据";
		var items = str.split(".");
		if (items.length > 1)
			return self.translateChinese(items[0], items[1]);
		else
			return self.translateChinese(str);
	}
	// 翻译成中文。
	Number.prototype.translateChinese = function(value1, value2) {
		var self = this;
		var reStr = [];
		var doubleNum=parseFloat(arguments.length==2?value1+"."+value2:value1);
		var items = value1.split("").reverse();
		if(doubleNum>self.option_add.maxNum){
			console.log('翻译的数字大于最大值')
			return "";
		}
		for (var i = 0; i < items.length; i++) {
			if (i % 4 == 1 && items[i] != 0)
				reStr.unshift(self.option_add.chineseNumberList[1][0]);
			else if (i % 4 == 2 && items[i] != 0)
				reStr.unshift(self.option_add.chineseNumberList[1][1]);
			else if (i % 4 == 3 && items[i] != 0)
				reStr.unshift(self.option_add.chineseNumberList[1][2]);
			else if (i % 4 == 0 && (i / 4) % 3 == 1 && i != 0){
				if(items[i]!=0||items[i+1]!=0||items[i+2]!=0||items[i+3]!=0)
					reStr.unshift(self.option_add.chineseNumberList[2][0]);
				if(items[i]==0&&items[i-1]!=0&&items[i+1]==0){
					reStr.unshift(self.option_add.chineseNumberList[0][items[i]])
				}
			}
			else if (i % 4 == 0 && (i / 4) % 3 == 2 && i != 0){
				reStr.unshift(self.option_add.chineseNumberList[2][1]);
				if(items[i]==0&&items[i-1]!=0&&items[i+1]==0){
					reStr.unshift(self.option_add.chineseNumberList[0][items[i]])
				}
			}
			else if (i % 4 == 0 && (i / 4) % 3 == 0 && i != 0){
				reStr.unshift(self.option_add.chineseNumberList[2][1]);
				if(items[i]==0&&items[i-1]!=0&&items[i+1]==0){
					reStr.unshift(self.option_add.chineseNumberList[0][items[i]])
				}
			}
			if (items[i] != 0)
				reStr.unshift(self.option_add.chineseNumberList[0][items[i]]);
			else if(items[i]==0){
				if(items[i-1]&&items[i-1]!=0&&items[i+1]==0&&i%4!=0){
					console.log("this is zero");
					reStr.unshift(self.option_add.chineseNumberList[0][items[i]]);
				}else if(i%4!=0&&items[i-1]!=0&&items[i+1]!=0){
					reStr.unshift(self.option_add.chineseNumberList[0][items[i]]);
				}
			}
		}
		if(value1!=0)
			reStr.push(self.option_add.chineseNumberList[3][0]);
		if (arguments.length == 2) {
			items = value2.split("");
			for (var i = 0; i < items.length; i++) {
				if (i == 2){
					console.log("填写了错误的长度");
					return " ";
					reStr.push("余");
				}
				if(items[i]!=0)
					reStr.push(self.option_add.chineseNumberList[0][items[i]]);
				if (i == 0&&items[i]!=0)
					reStr.push(self.option_add.chineseNumberList[3][i + 1]);
				if (i == 1&&items[i]!=0)
					reStr.push(self.option_add.chineseNumberList[3][i + 1]);
			}
		}
		return reStr.join("");
	}
	var ChineseAmount = function() {

	}
	ChineseAmount.valueOf = function(value) {
		return (value)?Number.prototype.inChinese(value):" ";
	}

	module.exports = ChineseAmount;
});